package de.baumann.browser.adapter;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import de.baumann.browser.OdinGlideModule;
import de.baumann.browser.R;
import de.baumann.browser.api.net.vo.Node;
import de.baumann.browser.utils.ActiveUtil;
import de.baumann.browser.utils.SPUtilKt;

public class NodeListAdapter extends BaseQuickAdapter<Node, BaseViewHolder> {

    private OnJoinNodeListener joinNodeListener;
    private boolean isShowJoin;

    public NodeListAdapter(int layoutResId, boolean isShowJoin) {
        super(layoutResId);
        this.isShowJoin = isShowJoin;
    }

    public void setJoinNodeListener(OnJoinNodeListener joinNodeListener) {
        this.joinNodeListener = joinNodeListener;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(BaseViewHolder helper, Node item) {
        helper.setText(R.id.tvNodeListName, item.getNodeName() + "超级节点");
        TextView itemTvNodeOwner = helper.getView(R.id.itemTvNodeOwner);
        TextView tvItemNodeMembers = helper.getView(R.id.tvItemNodeMembers);
        ImageView ivItemHostHead = helper.getView(R.id.ivItemHostHead);

        //超级节点活动关闭，隐藏队长信息
        if (ActiveUtil.INSTANCE.superNodeIsClose()) {
            itemTvNodeOwner.setVisibility(View.GONE);
            ivItemHostHead.setVisibility(View.GONE);
        }else{
            itemTvNodeOwner.setText("队长:  " + item.getOwnerName());
            OdinGlideModule.loadWithCropAndCircle(mContext, item.getOwnerAvatar(), ivItemHostHead, R.drawable.icon_head);
        }
        tvItemNodeMembers.setText(String.format("人数(%s)", item.getHeadcount()));
        helper.setText(R.id.tvItemTotalHash, item.getTotalContribute());
        TextView tvNodeTag = helper.getView(R.id.tvNodeTag);
        ImageView ivNodeTag = helper.getView(R.id.ivNodeTag);
        switch (helper.getLayoutPosition()) {
            case 0:
                ivNodeTag.setImageResource(R.drawable.supernode_icon_one);
                tvNodeTag.setVisibility(View.GONE);
                ivNodeTag.setVisibility(View.VISIBLE);
                break;
            case 1:
                ivNodeTag.setImageResource(R.drawable.supernode_icon_two);
                tvNodeTag.setVisibility(View.GONE);
                ivNodeTag.setVisibility(View.VISIBLE);
                break;
            case 2:
                ivNodeTag.setImageResource(R.drawable.supernode_icon_three);
                tvNodeTag.setVisibility(View.GONE);
                ivNodeTag.setVisibility(View.VISIBLE);
                break;
            default:
                ivNodeTag.setVisibility(View.INVISIBLE);
                tvNodeTag.setVisibility(View.VISIBLE);
                tvNodeTag.setText(String.valueOf(helper.getLayoutPosition() + 1));
        }

        ProgressBar progressBar = helper.getView(R.id.itemProgressNode);
        double totalContribute = Double.valueOf(item.getTotalContribute());
        progressBar.setProgress((int) totalContribute);

        Button btnJoinNode = helper.getView(R.id.btnJoinNode);
        if (!isShowJoin) {
            btnJoinNode.setVisibility(View.GONE);
        }

        btnJoinNode.setOnClickListener(v -> {
            if (joinNodeListener != null) {
                joinNodeListener.joinNode(item.getNodeId());
            }
        });
    }

    public interface OnJoinNodeListener {
        void joinNode(String nodeId);
    }

}
